package com.demo.springcloud.controller;

import com.demo.springcloud.entity.CommonResult;
import com.demo.springcloud.entity.User;
import com.demo.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController()
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 插入用户信息
     * @param user
     * @return
     */
    @PostMapping(value = "/user/saveUser")
    public CommonResult save(User user){
        int result = userService.save(user);
        log.info("插入结果为："+result);
        if (result>0){
            return new CommonResult(200,"插入成功",result);
        }else {
            return new CommonResult(444,"插入失败",null);
        }
    }
    @PutMapping("/user/updateUser")
    public CommonResult update(User user){
        int result = userService.update(user);
        log.info("更新结果为："+result);
        if (result>0){
            return new CommonResult(200,"更新数据成功",result);
        }else {
            return new CommonResult(444,"更新数据失败",null);
        }
    }

    @DeleteMapping("/user/deleteUser/{id}")
    public CommonResult deleteById(@PathVariable("id") Long id){
        int result = userService.deleteById(id);
        log.info("删除结果为："+result);
        if (result>0){
            return new CommonResult(200,"删除数据成功",result);
        }else {
            return new CommonResult(444,"删除数据失败",null);
        }
    }
    @GetMapping("/user/selectAllUser")
    public CommonResult selectAll(){
        List<User> users = userService.selectAll();
        log.info("查询的用户信息为:"+users);
        if (users != null){
            return new CommonResult(200,"查询数据成功",users);
        }else {
            return new CommonResult(444,"查询数据失败",null);
        }
    }
    @GetMapping("/user/selectById/{id}")
    public CommonResult<User> selectById(@PathVariable("id") Long id){
        User user = userService.selectById(id);
        log.info("查询的用户信息为:"+user);
        if (user != null){
            return new CommonResult(200,"查询数据成功",user);
        }else {
            return new CommonResult(444,"查询数据失败",null);
        }
    }
}
